// pages/draw/detail.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    siteinfo: require('../../siteinfo'),
    shareTitle: '',
    shareImage: '',
    timeline_share_id: 0,
    index_type: 'chat',
    content: null,
    token: '',

    id:'',
    info:{},
    show:false,

    isshow:false//是否是自己创作的
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id:options.id
    })
    app.util.request({
      url: '/huahua/detail',
      data:{id:options.id}
    }).then(res => {
      if(res.data.user_id ==  wx.getStorageSync('userid')){
        this.setData({
          isshow: true
        })
      }
      this.setData({
        info: res.data
      })
    })
  },
  copy() {
    wx.setClipboardData({
      data: this.data.info.content,
      success(res) {
        console.log('已复制')
      }
    })
  },
  draw(){
    wx.navigateTo({
      url: '/pages/draw/index?typeid='+this.data.info.huahua.type+'&sizeid='+
      this.data.info.huahua.size+'&styleid='+this.data.info.huahua.style+'&content='+this.data.info.content
    })
  },
  toShow(){
    this.setData({
      show:true
    })
  },
  toClose(){
    this.setData({
      show:false
    })
  },
  toShare() {
    let that = this;
    app.util.message('请点右上角“...”，转发给朋友', 'error', function () {
      that.setData({
        show:false
      })
    })
  },
  async copy2(){
    let that = this;
    var share_id = await this.getShareId('wechat');
    let url = that.data.siteinfo.host+'/pages/index/index?sid='+share_id
    // console.log(url)
    // return
    wx.setClipboardData({
      data: url,
      success(res) {
        that.setData({
          show:false
        })
        console.log('已复制')
      }
    })
  },
  toOpen(){
    app.util.request({
      url: '/user/open',
      data:{zid:this.data.id}
    }).then(res => {
      app.util.message(res.data);
    })
  },
  toPrint(){
    wx.navigateTo({
      url: '/pages/draw/print?id='+this.data.id
    })
  },
  main(){
    wx.reLaunch({
      url: '/pages/user/index',
    })
  },
  choose(e){
    let type = e.currentTarget.dataset.type;
    app.util.request({
      url: '/user/like',
      data:{zid:this.data.id,type:type}
    }).then(res => {
      app.util.message(res.data)
      if(type == 1){
        if(this.data.info.is_like==1){
          this.setData({
            ['info.is_like']:0
          })
        }else{
          this.setData({
            ['info.is_like']:1
          })
        }
      }else{
        if(this.data.info.is_coll==1){
          this.setData({
            ['info.is_coll']:0
          })
        }else{
          this.setData({
            ['info.is_coll']:1
          })
        }
      }
    })
  },
  
  async onShareAppMessage() {//右上角分享
    var share_id = await this.getShareId('wechat')
    console.log('sh', share_id)
    return {
      title: this.data.shareTitle,
      imageUrl: this.data.shareImage,
      path: '/pages/index/index?sid=' + share_id
    }
  },
  onShareTimeline() {//朋友圈
    var share_id = this.data.timeline_share_id
    if (!share_id) {
      this.getShareId('timeline')
    }
    console.log('share_id', share_id)
    return {
      title: this.data.shareTitle,
      imageUrl: this.data.shareImage,
      query: 'sid=' + share_id
    }
  },
  async getShareId(way) {
    var share_id = 0
    await app.util.request({
      url: '/wxapp/doShare',
      data: {
        way: way
      }
    }).then(res => {
      share_id = res.data.share_id
      if (way == 'timeline') {
        this.setData({
          timeline_share_id: share_id
        })
      }
    })
    return share_id
  },
  onAddToFavorites() {//收藏
    return {
      title: this.data.shareTitle,
      imageUrl: this.data.shareImage
    }
  }
  
})